package love.xin.util

import android.content.Context
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions

class PermissionUtil {
    companion object{

        val TAG = "xianban"
         val permissions = arrayOf(
            Permission.SCHEDULE_EXACT_ALARM,
             Permission.POST_NOTIFICATIONS,
             Permission.MANAGE_EXTERNAL_STORAGE

        )
        fun initPermission(context: Context, callback: PermissionCallback?,permission:Array<String>) {
            XXPermissions.with(context).permission(*permission)
                .request(object : OnPermissionCallback {
                    override fun onGranted(permissions: MutableList<String>, allGranted: Boolean
                    ) {
                        if (!allGranted) {
                            return
                        }
                        callback?.onPermissionGranted()
                    }

                    override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean
                    ) {
                        if (doNotAskAgain) {
                            XXPermissions.startPermissionActivity(context, permissions)
                        }
                    }
                })
        }
        fun initPermission(context: Context, callback: PermissionCallback?, permission:String) {
            XXPermissions.with(context).permission(permission)
                .request(object : OnPermissionCallback {
                    override fun onGranted(permissions: MutableList<String>, allGranted: Boolean
                    ) {
                        if (!allGranted) {
                            return
                        }
                        callback?.onPermissionGranted()
                    }

                    override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean
                    ) {
                        if (doNotAskAgain) {
                            XXPermissions.startPermissionActivity(context, permissions)
                        }
                    }
                })
        }
    }
}

interface PermissionCallback {
    fun onPermissionGranted()
}